﻿// <copyright file="Candles.cs" company="Golizza.net">Copyright (c) 2012 All Right Reserved</copyright>

namespace ClietnApp.Model
{
    using System;
    using System.Collections.ObjectModel;
    using Common;

    /// <summary>
    /// Model for the candle chart series.
    /// </summary>
    public class Candles : NotifyPropertyChangedBase
    {
        #region [ Private Fields ]

        /// <summary>
        /// The back up value for <see cref="CandleCollection"/> property.
        /// </summary>
        private ObservableCollection<Candle> candleCollection;

        /// <summary>
        /// The back up value for <see cref="FirstCurrency"/> property.
        /// </summary>
        private string firstCurrency;

        /// <summary>
        /// The back up value for <see cref="SecondCurrency"/> property.
        /// </summary>
        private string secondCurrency;

        /// <summary>
        /// The back up value for <see cref="MinValue"/> property.
        /// </summary>
        private double minValue;

        /// <summary>
        /// The back up value for <see cref="MaxValue"/> property.
        /// </summary>
        private double maxValue;

        #endregion

        #region [ Properties ]

        /// <summary>
        /// Gets the candle collection.
        /// </summary>
        /// <value>
        /// The candle collection.
        /// </value>
        public ObservableCollection<Candle> CandleCollection
        {
            get
            {
                return this.candleCollection ?? (this.candleCollection = new ObservableCollection<Candle>());
            }
        }

        /// <summary>
        /// Gets or sets the first currency.
        /// </summary>
        /// <value>
        /// The first currency.
        /// </value>
        public string FirstCurrency
        {
            get
            {
                return this.firstCurrency;
            }

            set
            {
                if (this.firstCurrency != value)
                {
                    this.firstCurrency = value;
                    this.OnPropertyChanged("FirstCurrency");
                }
            }
        }

        /// <summary>
        /// Gets or sets the second currency.
        /// </summary>
        /// <value>
        /// The second currency.
        /// </value>
        public string SecondCurrency
        {
            get
            {
                return this.secondCurrency;
            }

            set
            {
                if (this.secondCurrency != value)
                {
                    this.secondCurrency = value;
                    this.OnPropertyChanged("SecondCurrency");
                }
            }
        }

        /// <summary>
        /// Gets the min value.
        /// </summary>
        /// <value>
        /// The min value.
        /// </value>
        public double MinValue
        {
            get
            {
                return this.minValue;
            }

            private set
            {
                if (this.minValue != value)
                {
                    this.minValue = value;
                    this.OnPropertyChanged("MinValue");
                }
            }
        }

        /// <summary>
        /// Gets the max value.
        /// </summary>
        /// <value>
        /// The max value.
        /// </value>
        public double MaxValue
        {
            get
            {
                return this.maxValue;
            }

            private set
            {
                if (this.maxValue != value)
                {
                    this.maxValue = value;
                    this.OnPropertyChanged("MaxValue");
                }
            }
        }

        #endregion

        #region [ Public Methods ]

        /// <summary>
        /// Adds the specified value.
        /// </summary>
        /// <param name="candle">The value.</param>
        public void Add(Candle candle)
        {
            this.CandleCollection.Add(candle);

            this.MinValue = Math.Min(this.MinValue, Math.Min(candle.Low, Math.Min(candle.High, Math.Min(candle.Close, candle.Open))));
            this.MaxValue = Math.Max(this.MaxValue, Math.Max(candle.Low, Math.Max(candle.High, Math.Max(candle.Close, candle.Open))));
        }

        #endregion
    }
}
